package com.service.admin.news.controller;

import com.service.admin.news.controller.rep.NewsDetailRep;
import com.service.admin.news.controller.rep.NewsPageRep;
import com.service.admin.news.controller.req.NewsAddReq;
import com.service.admin.news.controller.req.NewsPageReq;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.OneResponse;
import com.service.core.rest.response.PageResponse;
import com.service.admin.news.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 系统公告
 *
 * @author xxxx
 * @email xxxx@qq.com
 * @date 2019-11-12 21:59:44
 */
@Slf4j
@RestController
@RequestMapping("news")
public class NewsController extends BaseController {
    @Autowired
    private NewsService newsService;

    @PostMapping("/add")
    public BaseResponse add(@RequestBody @Validated NewsAddReq newsAddReq) {
        log.info("NewsController.add start,newsAddReq:{}", newsAddReq);
        newsAddReq.setId(null);
        newsService.addNews(newsAddReq);
        return successSaveBaseResponse();
    }

    @PostMapping("/update/{newsId}")
    public BaseResponse update(@PathVariable Integer newsId, @RequestBody @Validated NewsAddReq newsAddReq) {
        newsAddReq.setId(newsId);
        newsService.updateNews(newsAddReq);
        return successSaveBaseResponse();
    }

    //todo 列表 不要内容
    @PostMapping("/page")
    public PageResponse<NewsPageRep> page(@RequestBody NewsPageReq newsPageReq) {
        log.info("NewsController.page start,newsPageReq:{}", newsPageReq);
        return successQueryPageResponse(newsService.pageNews(newsPageReq));
    }

    // 删除
    @PutMapping("/delete/{newsId}")
    public BaseResponse deleteNews(@PathVariable Integer newsId) {
        log.info("NewsController.deleteNews start,newsId:{}", newsId);
        newsService.deleteNews(newsId);
        return successDeleteBaseResponse();
    }

    @GetMapping("/detail/{newsId}")
    public OneResponse<NewsDetailRep> newsDetail(@PathVariable Integer newsId) {
        log.info("NewsController.newsDetail start,newsId:{}", newsId);
        return successQueryOneResponse(newsService.getOneNewsDetail(newsId));
    }
}
